[contents] [prev] [next] [top] [bottom] (11 out of 17)

Arithmetic

The ScriptX language includes the following infix operators for simple arithmetic:

+

addition

-

subtraction

*

multiplication

/

division

Each of these operators requires two operands. When two operands for an arithmetic expression are of different number classes, one operand may be promoted internally to accommodate the range of the other class.

The following examples demonstrate the promotion of x, which is an ImmediateInteger object. The getClass method is defined by RootObject, the root class from which all ScriptX classes inherit, so it is available to all objects.

global x:2, y:3.5, z:123.456789, w:9876543210, a, b, c
getClass x
ImmediateInteger
getClass y
ImmediateFloat
getClass z
Float
getClass w
LargeInteger
a := x + y
5.5
getClass a
ImmediateFloat
b := x + z
125.456789
getClass b
Float
c := x + w
9876543212
getClass c
LargeInteger

The result may be demoted for efficiency, if there is no loss of range.

global p:3.23, q:4.23, resultObject
getClass p
ImmediateFloat
getClass q
ImmediateFloat
resultObject := p - q
1
getClass resultObject
ImmediateInteger 

String Arithmetic

You can use the addition and subtraction operators on strings.

String addition concatenates its operands into a single string:

"this" + "that"
"thisthat"
"Once upon " + "a time"
"Once upon a time"
String subtraction removes the first instance of the second string operand from the first:

"one two three" - "one"
" two three"
"banana" - "an"
"bana"

The following code illustrates an easy way to print out the value of a variable. Note that the parentheses are necessary and that the integer x must be coerced to a String object in order to be added to another string.

x := 10
print ("x: "+ x as String)
"x: 10"

String subtraction can make stripping the extension away from a file name easy:

myFilename := "Answers.doc"
myPrefix := myFilename - ".doc"
"Answers"

In these examples, string addition and subtraction convert the original string, an instance of the class StringConstant, into an instance of the class String (its editable counterpart). StringConstant is a subclass of String. Operations that specifically test for an object's class may not consider String and StringConstant to be the same, even though they are both strings.


This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.

Copyright 1996 Apple Computer, Inc. All Rights Reserved.